<?php

namespace App\Listeners;

use App\Ant\Excel\Imports\FurnitureImport;
use App\Ant\Excel\Imports\LabelImport;
use App\Ant\Excel\Imports\MaterialImport;
use App\Events\TaskEvent;
use App\Models\Company\Task\Furniture;
use App\Models\LabelTask;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Maatwebsite\Excel\Facades\Excel;

class Task implements ShouldQueue
{

    /**
     * The name of the connection the job should be sent to.
     *
     * @var string|null
     */
    public $connection = 'redis';

    /**
     * The name of the queue the job should be sent to.
     *
     * @var string|null
     */
    public $queue = null;

    /**
     * The time (seconds) before the job should be processed.
     *
     * @var int
     */
    public $delay = 10;

    /**
     * 任务可以尝试的最大次数。
     *
     * @var int
     */
    public $tries = 1;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param object $event
     * @return void
     */
    public function handle(TaskEvent $event)
    {
        if ($event->model instanceof LabelTask) {
            Excel::import(new LabelImport($event->model), 'public/' . $event->model->path);
            return true;
        } else if ($event->model instanceof Furniture) {
            Excel::import(new FurnitureImport($event->model), 'public/' . $event->model->path);
            return true;
        }
        Excel::import(new MaterialImport($event->model), 'public/' . $event->model->path);
    }
}
